package pl.gladiator.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import pl.gladiator.model.Newsy;

public class NewsyEntityManagerImp implements NewsyEntityManager{
    public static Logger log = Logger.getLogger(NewsyEntityManagerImp.class);
    private Connection connect;
    private ResultSet result = null;
	private PreparedStatement preparedStatement = null;
	
	public void connectWithDB(){
		try {
			String url = "jdbc:mysql://misiekmk.linuxpl.info/misiekmk_uks?useOldAliasMetadataBehavior=true&characterEncoding=utf8&" + "user=misiekmk_uks&password=uks";
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection(url);
	        if(log.isInfoEnabled())log.info("Polaczono z BD");
		} catch (ClassNotFoundException e) {
			log.fatal(e.getMessage());
			e.printStackTrace();
		} catch (SQLException e) {
			log.fatal(e.getMessage());
			e.printStackTrace();
		}
       
	}
	
	@Override
	public void save(Newsy news) {
		try {
			connectWithDB();
			
			preparedStatement = connect.prepareStatement("insert into newsy values(default, ?, ?, now())");
            preparedStatement.setString(1, news.getSubject());
            preparedStatement.setString(2, news.getContents());
            preparedStatement.executeUpdate();
			
			if(this.preparedStatement != null){
				this.preparedStatement.close();
			}
			
		} catch (SQLException e) {
			log.fatal(e.getMessage());
		}finally{
			if(connect != null){
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	@Override
	public List<Newsy> getAllNews() {
		List<Newsy> newsList = new ArrayList<Newsy>();
		connectWithDB();
		try {
			this.preparedStatement = this.connect.prepareStatement("select * from newsy");
			result = this.preparedStatement.executeQuery();
			while(result.next()){
				Date newsDate = result.getDate("newsDate");
				Newsy news = new Newsy(result.getInt("newsPk"), result.getString("subject"), result.getString("contents"), newsDate);
				newsList.add(news);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}finally{
			if(connect != null){
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return newsList;
	}
	
	public Newsy findNewsByPk(int pk){
		connectWithDB();
		Newsy news = null;
		try {
			this.preparedStatement = this.connect.prepareStatement("select * from newsy where newsPk = " + pk);
			result = this.preparedStatement.executeQuery();
			while(result.next()){
				Date newsDate = result.getDate("newsDate");
				news = new Newsy(result.getInt("newsPk"), result.getString("subject"), result.getString("contents"), newsDate);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}finally{
			if(connect != null){
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
		return news;
	}

	@Override
	public void delete(int pk) {
		connectWithDB();
		String query = "delete from newsy where newsPk = ?";
		PreparedStatement st;
		try {
			st = connect.prepareStatement(query);
			st.setObject(1, pk);

			st.executeUpdate();
			st.close();
		} catch (SQLException e) {
			if(log.isDebugEnabled()) log.debug(e.getMessage());
			e.printStackTrace();
		}finally{
			if(connect != null){
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	@Override
	public void update(Newsy news) {
		try {
			connectWithDB();
			
			String query  = "update newsy set subject = ?, contents = ? where newsPk = ?";
			PreparedStatement st = connect.prepareStatement(query);

			st.setString(1, news.getSubject());
			st.setString(2, news.getContents());
			st.setInt(3, news.getNewsPk());

			st.executeUpdate();
			st.close();
			
			if(this.preparedStatement != null){
				this.preparedStatement.close();
			}
			
		} catch (SQLException e) {
			log.fatal(e.getMessage());
		}finally{
			if(connect != null){
				try {
					connect.close();
					log.info("Rozlaczono z BD");
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			}
		}
	}
	
}
